package com.ipet.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;

import com.afollestad.materialdialogs.MaterialDialog;
import com.ipet.app.R;
import com.ipet.app.api.BaseCallBack;
import com.ipet.app.api.RequestManager;
import com.ipet.app.bean.UserInfo;
import com.ipet.app.bean.apiRequestBean.LoginRequest;
import com.ipet.app.bean.apiResponseBean.LoginResponse;
import com.ipet.app.bean.apiResponseBean.RegisterResponse;
import com.ipet.app.service.dao.DbHelper;
import com.ipet.app.ui.base.BaseActivity;
import com.ipet.app.util.CheckUtil;

import shem.com.materiallogin.MaterialLoginView;
import shem.com.materiallogin.MaterialLoginViewListener;

/**
 * Created by jindongping on 16/3/8.
 */
public class LoginActivity extends BaseActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        final MaterialLoginView login = (MaterialLoginView) findViewById(R.id.login);
        login.setListener(new MaterialLoginViewListener() {
                              @Override
                              public void onRegister(TextInputLayout registerUser, TextInputLayout registerPass, TextInputLayout registerPassRep) {
                                  dealRegister(registerUser, registerPass, registerPassRep, login);
                              }

                              @Override
                              public void onForgetPassword() {
                                  //
                                  Snackbar.make(login, getResources().getString(R.string.forget_password), Snackbar.LENGTH_SHORT).show();
                              }

                              @Override
                              public void onUserAgreement() {
                                  //
                                  Snackbar.make(login, getResources().getString(R.string.user_agreement), Snackbar.LENGTH_SHORT).show();
                              }

                              @Override
                              public void onLogin(TextInputLayout loginUser, TextInputLayout loginPass) {
                                  dealLogin(loginUser, loginPass, login);
                              }
                          }

        );


    }

    /**
     * 处理等登录事件
     *
     * @param loginUser 手机号输入框
     * @param loginPass 密码输入框
     * @param login     loginView
     */
    private void dealLogin(TextInputLayout loginUser, TextInputLayout loginPass, final MaterialLoginView login) {
        final String user = loginUser.getEditText().getText().toString();
        if (user.isEmpty()) {
            loginUser.setError(getResources().getString(R.string.phone_number_no_empty));
            return;
        }
        //判断手机号是否正确
        if (!CheckUtil.isMobileNO(user)) {
            loginUser.setError(getResources().getString(R.string.phone_number_wrong));
            return;
        }
        loginUser.setError("");
        String pass = loginPass.getEditText().getText().toString();
        loginPass.setError("");
        //登陆请求
        MaterialDialog dialog = showLoadingDialogNoTitle("登陆中...");
        RequestManager.getApi().login(new LoginRequest(user, pass)).enqueue(new BaseCallBack<LoginResponse>(login, dialog) {
            @Override
            public void onRequestSuccess(LoginResponse response) {
                //保存到数据库
                new DbHelper<UserInfo>().createOrUpdate(response.getUserInfo());
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * @param registerUser    手机号注册框
     * @param registerPass    用户密码框
     * @param registerPassRep 确认密码框
     * @param login           loginView
     */
    private void dealRegister(TextInputLayout registerUser, TextInputLayout registerPass, TextInputLayout registerPassRep, final MaterialLoginView login) {
        String user = registerUser.getEditText().getText().toString();
        if (user.isEmpty()) {
            registerUser.setError(getResources().getString(R.string.phone_number_no_empty));
            return;
        }
        //判断手机号是否正确
        if (!CheckUtil.isMobileNO(user)) {
            registerUser.setError(getResources().getString(R.string.phone_number_wrong));
            return;
        }
        registerUser.setError("");

        String pass = registerPass.getEditText().getText().toString();
        if (pass.isEmpty()) {
            registerPass.setError(getResources().getString(R.string.repeat_password));
            return;
        }
        registerPass.setError("");

        String passRep = registerPassRep.getEditText().getText().toString();
        if (!pass.equals(passRep)) {
            registerPassRep.setError(getResources().getString(R.string.diff_two_password));
            return;
        }
        registerPassRep.setError("");
        //注册请求
        MaterialDialog dialog = showLoadingDialogNoTitle("注册中...");
        RequestManager.getApi().registr(user, pass).enqueue(new BaseCallBack<RegisterResponse>(login, dialog) {
            @Override
            public void onRequestSuccess(RegisterResponse response) {
                //保存到数据库
                new DbHelper<UserInfo>().createOrUpdate(response.getUserInfo());
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

}
